package com.example.mannortycoon.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.example.mannortycoon.entity.UserBuySeedRecordEntity;
import com.example.mannortycoon.entity.UserSeedEntity;
import com.example.mannortycoon.service.SeedService;
import com.example.mannortycoon.service.UserSeedService;
import com.example.mannortycoon.service.UserService;
import com.example.mannortycoon.uitl.JWTUtils;
import com.example.mannortycoon.uitl.PageUtils;
import com.example.mannortycoon.uitl.R;
import com.example.mannortycoon.vo.UserSeedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("game/userSeed")
@Api(tags="用户种子")
public class UserSeedController {

    private final UserSeedService userSeedService;


    @Autowired
    public UserSeedController(UserSeedService userSeedService){
        this.userSeedService = userSeedService;
    }

    /**
     * app用户购买种子
     */
    @PostMapping("/buySeed")
    @ApiOperation(value = "用户购买种子")
    public R buySeed(HttpServletRequest request, @RequestBody UserBuySeedRecordEntity userBuySeedRecordEntity){
        Long id = JWTUtils.getId(request.getHeader("token"));
        if(id == 0){
            return R.error("token无效");
        }
        //判断用户种子币是否足够，足够则购买，不足够则给出对应的提示
        userBuySeedRecordEntity.setUserId(id);
        return userSeedService.save(userBuySeedRecordEntity);
    }

    /**
     * app销售种子
     */
    @PostMapping("/sellSeed/{seedId}/{number}")
    @ApiOperation(value = "用户销售种子")
    public R sellSeed(HttpServletRequest request,@PathVariable("seedId") Long seedId,@PathVariable("number")Integer number){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        return userSeedService.sellSeed(userId,seedId,number);
    }

    /**
     * app获取用户种子列表
     */
    @GetMapping("/getUserSeedList")
    @ApiOperation("获取用户种子列表")
    public R getUserSeedList(HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        List<UserSeedVo> list = userSeedService.getUserSeedList(userId);
        return R.ok().put("data",list);
    }

    /**
     * 用户解锁种子
     */
    @PutMapping("/unlockSeed/{seedId}")
    @ApiOperation("用户解锁种子")
    public R unlockSeed(@PathVariable("seedId")Long seedId ,HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        return userSeedService.unlockSeed(userId,seedId);
    }


    /**
     * 后台获取用户种子信息
     */
    @GetMapping("/info/{id}")
    @ApiIgnore
    public R info(@PathVariable("id") Long id){
        List<UserSeedVo> list = userSeedService.getUserSeedList(id);
        return R.ok().put("data", list);
    }

    /**
     * 后台保存用户种子信息
     */
    @PutMapping("/save/{userId}")
    @ApiIgnore
    public R update(@PathVariable("userId") Long userId, @RequestBody List<UserSeedEntity> userSeedList){
		userSeedService.saveUserSeedList(userId,userSeedList);
        return R.ok();
    }
}
